#! /usr/bin/env bash

case "$(os -m)" in
  freebsd|openbsd|darwin)
    ip=`ifconfig | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2 }' | head -n 1`

    [ -z $? ] || {
        echo -n "${ip}"
        #external_ip=`wget icanhazip.com -q -O -`
        external_ip=`curl icanhazip.com`
        echo " -> ${external_ip}"
        exit 0
    }

    [[ -z `which networksetup` ]] && { exit; }

    IFS_BAK=$IFS
    IFS="
"

    ethernet=`networksetup -getinfo Ethernet 2>/dev/null`

    for entry in $ethernet; do
        if [[ $entry == IP\ address:\ * ]]; then
           ip="${entry##IP address: }"
           break
        fi
    done

    airport=`networksetup -getinfo AirPort 2>/dev/null`

    for entry in $airport; do
        if [[ $entry == IP\ address:\ * ]]; then
           ip="${entry##IP address: }"
           break
        fi
    done

    IFS=$IFS_BAK
    ;;
  solaris)
    ip=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2 }' | head -n 1`
    ;;
  *)
    ip=`ifconfig | grep 'inet addr:' | grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1 }' | head -n 1`
  ;;
esac

echo -n "${ip}"
#external_ip=`wget icanhazip.com -q -O -`
external_ip=`curl icanhazip.com`
echo " -> ${external_ip}"

